<template>
  <el-card>
    <!-- 面板屑导航 -->
    <el-breadcrumb separator="/" style="margin-bottom: 40px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="form-box">
      <!-- 表单区域 -->
      <el-form
        ref="searchRef"
        :model="q"
        label-width="80px"
        inline
        size="small"
      >
        <el-form-item label="文章分类">
          <el-select v-model="q.cate_id" placeholder="请选择分类">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.cate_name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布状态">
          <el-select v-model="q.state" placeholder="请选择状态">
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">筛选</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" size="mini" @click="pubDialogVisible = true"
        >发表文章</el-button
      >
    </div>
    <!-- 表格 -->
    <el-table :data="artList" style="width: 100%" stripe border>
      <el-table-column prop="title" label="文章标题" width="180">
        <template v-slot="{ row }">
          <el-link type="primary" @click="showInfo(row.id)">{{
            row.title
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类" width="180">
      </el-table-column>
      <!-- <el-table-column prop="pub_date" label="发表时间"></el-table-column> -->
      <el-table-column label="发表时间">
        <template #default="{ row }">
          {{ row.pub_date | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="danger" size="mini" @click="remove(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="q.pagenum"
      :page-sizes="[2, 3, 5, 10]"
      :page-size="q.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 文章详情的对话框 -->
    <!-- 文章详情的对话框 -->
    <el-dialog title="文章预览" :visible.sync="infoVisible" width="80%">
      <ArtInfo :artInfo="artInfo"></ArtInfo>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="infoVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

    <!-- 发表文章 -->
    <el-dialog
      title="发表文章"
      :visible.sync="pubDialogVisible"
      :fullscreen="true"
      :before-close="beforeClose"
      destroy-on-close
      width="70%"
    >
      <ArtPub @addClose="addCloseDialog"></ArtPub>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  reqGetArticleList,
  reqDelArticleById,
  reqGetArtInfo
} from '@/api/article'
import { mapState, mapActions } from 'vuex'
import ArtInfo from './components/ArtInfo.vue'
import ArtPub from './components/ArtPub.vue'
export default {
  components: {
    ArtInfo,
    ArtPub
  },
  data () {
    return {
      // 查询参数 q
      q: {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
      },
      // 文章列表的数组
      artList: [],
      total: 0,
      infoVisible: false,
      // 文章详情对象
      artInfo: {},
      /* 发表文章 */
      pubDialogVisible: false
    }
  },
  computed: {
    ...mapState('article', ['cateList']) // 文章分类数据
  },
  created () {
    this.getCateList()
    this.initArtList()
  },
  methods: {
    ...mapActions('article', ['getCateList']),
    // 初始化文章列表的数据
    async initArtList () {
      const { data: res } = await reqGetArticleList(this.q)

      if (res.code !== 0) return this.$message.error('获取文章列表失败！')

      this.artList = res.data
      this.total = res.total
    },
    search () {
      this.q.pagenum = 1
      this.initArtList()
    },
    reset () {
      this.q.pagenum = 1
      this.q.cate_id = ''
      this.q.state = ''
      this.initArtList()
    },
    // 每页显示的数据条数发生了变化
    handleSizeChange (val) {
      this.q.pagesize = val
      this.initArtList()
    },
    // 页码值发生了变化
    handleCurrentChange (val) {
      this.q.pagenum = val
      this.initArtList()
    },
    // 根据 id 删除文章
    async remove (id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文章, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') return this.$message.info('已取消操作')

      const { data: res } = await reqDelArticleById(id)

      if (res.code !== 0) return this.$message.error('删除文章失败！')
      this.$message.success('删除文章成功！')

      // 如果当前页码值 > 1 并且当前页中数据的条数等于 1
      if (this.q.pagenum > 1 && this.artList.length === 1) {
        // 则删除成功之后，当前页的数据条数为 0，需要请求上一页数据
        this.q.pagenum--
      }
      this.initArtList()
    },
    async showInfo (id) {
      console.log(id)
      const { data } = await reqGetArtInfo(id)
      if (data.code !== 0) return this.$message.error('文章预览失败！')
      this.artInfo = data.data
      this.infoVisible = true
    },
    // 关闭发布文章的对话框前的函数
    async beforeClose (done) {
      // 确认消息提示
      const str = await this.$confirm('此操作将退出发布, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消操作')
      // done调用会关闭对话框
      done()
    },
    // 发布文章完毕关闭对话框
    addCloseDialog () {
      this.pubDialogVisible = false
      // 重新获取数据
      this.initArtList()
    }
  }
}
</script>
<style lang="less" scoped>
.form-box {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}
</style>
